package com.mall.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.db.domain.Address;
import com.mall.db.service.AddressService;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/address/ra")
public class AddressController extends BaseController<Address>{

    @Resource(name="addressService")
    private AddressService ras;

    @Override
    public Map<String, Object> find(Address address) {
        //实现分页
        PageHelper.startPage(address.getPage(),address.getLimit());

        QueryWrapper<Address> qw = new QueryWrapper<>();
        if (!StringUtils.isEmpty(address.getUserId())){
            qw.eq("user_id",address.getUserId());
        }if (!StringUtils.isEmpty(address.getReceiver())){
            qw.eq("receiver",address.getReceiver());
        }
        List<Address> list = ras.list(qw);

        PageInfo<Address> page = new PageInfo<>(list,address.getLimit());

        return this.findMap(page.getList(), page.getTotal());
    }

    @Override
    public Map<String, Object> add(Address address) {
        boolean b = ras.save(address);
        return this.iud("新增",b,address);
    }

    @Override
    public Map<String, Object> update(Address address) {
        boolean b = ras.updateById(address);
        return this.iud("编辑",b,address);
    }
    @Override
    public Map<String, Object> delete(Address address) {
        boolean b = ras.removeById(address);
        return this.iud("删除",b,address);
    }

    @RequestMapping("order/find")
    public Map<String, Object> orderFind(Address address) {
        PageHelper.startPage(address.getPage(),address.getLimit());
        QueryWrapper qw=new QueryWrapper();
        if(!StringUtils.isEmpty(address.getId())) {
            qw.eq("id",address.getId());
        }
        List<Address> list=ras.list(qw);
        PageInfo<Address> page=new PageInfo<>(list,address.getLimit());
        return findMap(page.getList(),page.getTotal());
    }

}
